//
//  HomeCollectionCell.swift
//  eBooks
//
//  Created by JunMing on 2019/10/11.
//  Copyright © 2019 赵俊明. All rights reserved.
//

import UIKit
import YYKit
import SnapKit

class HomeCollectionCell: UICollectionViewCell {
    private var cover:ImageLayerView!
    private var bookName:UILabel!
    private var dataSize:UILabel!
    private var author:UILabel!
    private var readRate:UILabel!
    private var share:UIButton!
    private var save:UIButton!
    private var line:UIView!
    typealias block = (_ model:Model, _ share:Bool) -> Void
    var touchCellBlock:block?
    
    var model:Model? {
        willSet {
            bookName.text = newValue?.title()
            if let auth = newValue?.author { author.text = auth + " 著" }
            if let size = newValue?.sizeStr() { dataSize.text = size }
            let image = UIImage(named: newValue!.isSave ? "minus":"plus")
            save.setImage(image?.origin, for: .normal)
            cover.loadImage(url: newValue!.coverurl())
            if let rate = newValue?.readRate { readRate.text = "已读:\(rate)%" }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubViews()
        // layoutHorizontal()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func shareAction() {
        print("share Tapped")
        routerEvent(eventName: kShareEventName, info: model!)
        touchCellBlock?(model!,true)
    }
    
    @objc func saveAction() {
        print("save Tapped")
        routerEvent(eventName: kSaveEventName, info: model!)
        touchCellBlock?(model!,false)
    }
    
    func setupSubViews() {
        cover = ImageLayerView(frame: .zero)
        cover.layer.cornerRadius = 0
        cover.layer.masksToBounds = false
        contentView.addSubview(cover)
        
        save = UIButton(type: .system)
        save.isHidden = false
        save.addTarget(self, action: #selector(saveAction), for: .touchUpInside)
        contentView.addSubview(save)
        
        bookName = labelCreate(font: UIFont.medium(14), tColor:UIColor.black)
        contentView.addSubview(bookName)
        
        dataSize = labelCreate(font: UIFont.regular(12))
        dataSize.isHidden = false
        contentView.addSubview(dataSize)
        
        author = labelCreate(font: UIFont.regular(12))
        contentView.addSubview(author)
        
        readRate = labelCreate(font: UIFont.regular(12))
        readRate.isHidden = false
        contentView.addSubview(readRate)
        
        line = UIView()
        line.isHidden = false
        line.alpha = 0.6
        line.backgroundColor = UIColor.RGB(200, 200, 200)
        contentView.addSubview(line)
        
        share = UIButton(type: .system)
        share.addTarget(self, action: #selector(shareAction), for: .touchUpInside)
        contentView.addSubview(share)
    }
    
    func layoutVertical() {
        cover.snp.remakeConstraints { (make) in
            make.width.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self).offset(-50)
        }
        
        author.snp.remakeConstraints { (make) in
            make.width.equalTo(self)
            make.bottom.equalTo(snp.bottom)
            make.height.equalTo(17)
        }
        
        bookName.snp.remakeConstraints { (make) in
            make.width.equalTo(self)
            make.top.equalTo(cover.snp.bottom).offset(3)
            make.bottom.equalTo(author.snp.top)
        }
        
        share.snp.remakeConstraints { (make) in
            make.right.equalTo(self.snp_right).offset(-3)
            make.top.equalTo(self).offset(3)
            make.height.width.equalTo(25)
        }
    }
    
    func layoutHorizontal() {
        cover.snp.remakeConstraints { (make) in
            make.left.equalTo(self).offset(10)
            make.width.equalTo(58)
            make.top.equalTo(self)
            make.bottom.equalTo(self).offset(-7)
        }
        
        share.snp.remakeConstraints { (make) in
            make.right.equalTo(self.snp_right).offset(-10)
            make.top.equalTo(self).offset(7)
            make.height.width.equalTo(25)
        }
        
        save.snp.remakeConstraints { (make) in
            make.right.equalTo(self).offset(-8)
            make.bottom.equalTo(self.snp_bottom).offset(-5)
            make.height.width.equalTo(30)
        }
        
        bookName.snp.remakeConstraints { (make) in
            make.left.equalTo(cover.snp_right).offset(10)
            make.right.equalTo(share.snp_left).offset(-50)
            make.top.equalTo(cover)
            make.height.equalTo(23)
        }
        
        author.snp.remakeConstraints { (make) in
            make.left.right.equalTo(bookName)
            make.top.equalTo(bookName.snp.bottom).offset(2)
            make.height.equalTo(17)
        }
        
        readRate.snp.remakeConstraints { (make) in
            make.left.equalTo(bookName.snp.left)
            make.top.equalTo( author.snp.bottom)
            make.height.equalTo(17)
            make.width.equalTo(80)
        }
        
        dataSize.snp.remakeConstraints { (make) in
            make.left.right.equalTo(readRate)
            make.height.equalTo(17)
            make.top.equalTo(readRate.snp.bottom)
        }
        
        line.snp.remakeConstraints { (make) in
            make.height.equalTo(1)
            make.width.equalTo(self)
            make.bottom.equalTo(snp.bottom)
        }
    }

    func labelCreate(textAlig:NSTextAlignment = .left,font:UIFont?,tColor:UIColor = .gray) -> UILabel {
        let label = UILabel()
        label.numberOfLines = 0
        label.textColor = tColor
        label.font = font
        label.textAlignment = textAlig
        return label
    }
    
    func switchVerticalHorizontal() {
        if Tools.getVertical() {
            share.setImage(UIImage(named: "moreicon")?.origin, for: .normal)
            layoutVertical()
            cover.layer.cornerRadius = 8
            cover.layer.masksToBounds = true
            save.isHidden = true
            dataSize.isHidden = true
            readRate.isHidden = true
            line.isHidden = true
        }else {
            share.setImage(UIImage(named: "moreShare")?.origin, for: .normal)
            layoutHorizontal()
            cover.layer.cornerRadius = 0
            cover.layer.masksToBounds = false
            save.isHidden = true
            dataSize.isHidden = false
            readRate.isHidden = false
            line.isHidden = false
        }
    }
}
